package com.aaa.sbm.service.impl;

import com.aaa.sbm.common.ResultUtil;
import com.aaa.sbm.dao.StockInApprovalDao;
import com.aaa.sbm.dao.StockInDao;
import com.aaa.sbm.dao.StockInDetailDao;
import com.aaa.sbm.entity.Product;
import com.aaa.sbm.entity.StockIn;
import com.aaa.sbm.entity.StockInApproval;
import com.aaa.sbm.entity.StockInDetail;
import com.aaa.sbm.service.StockInApprovalService;
import com.aaa.sbm.service.StockInService;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

@Service
public class StockInServiceImpl implements StockInService {

    @Resource
    private StockInDao stockInDao;

    @Resource
    private StockInDetailDao stockInDetailDao;

    @Resource
    private StockInApprovalDao stockInApprovalDao;

    Integer id;

    @Override
    public int insertSatockIn(StockIn stockIn) {
        int i = stockInDao.insertSatockIn(stockIn);
        id= stockIn.getId();
        return i;
    }

    @Override
    public Result insertStockInDetailIn(StockInDetail stockInDetail) {
        // 获取当前日期
        Date currentDate = new Date();
        int num=stockInDetail.getQuantity();
        Double price=stockInDetail.getPrice();
        stockInDetail.setAmount(num*price);

        StockIn stockIn = new StockIn();
        stockIn.setInCode(null);
        stockIn.setTotalAmount(stockInDetail.getAmount());
        stockIn.setSupplier(stockInDetail.getSupplier());
        stockIn.setStatus(2);
        stockIn.setCreateTime(currentDate);
        stockIn.setCompleteTime(null);
        stockIn.setRemark(stockInDetail.getRemark());
        this.insertSatockIn(stockIn);
        stockInDetail.setInId(id);
        int i = stockInDetailDao.insertStockInDetailIn(stockInDetail);
        return i>0?ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @Override
    public Result getStockList(Page<StockInDetail> page) {
        System.out.println(page.getPageNo()+" ----- "+page.getPageSize());
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        List<StockInDetail> list = stockInDetailDao.selectList(page.getData());
        PageInfo<StockInDetail> pageInfo = new PageInfo<>(list);
        return ResultUtil.success(pageInfo);
    }

    @Override
    public Result updateStockIn(StockInDetail stockInDetail) {
        StockInApproval stockInApproval = new StockInApproval();
        stockInApproval.setProductId(stockInDetail.getProductId());
        stockInApproval.setQuantity(stockInDetail.getQuantity());
        stockInApproval.setPrice(stockInDetail.getPrice());
        stockInApproval.setAmount(stockInDetail.getAmount());
        stockInApproval.setSupplier(stockInDetail.getSupplier());
        stockInApproval.setStatus(0);
        stockInApproval.setCreateTime(new Date());
        stockInApproval.setRemark(stockInDetail.getRemark());
        stockInApproval.setSdId(stockInDetail.getId());
        int rows = stockInApprovalDao.insertStockInApproval(stockInApproval);
        return rows > 0 ? ResultUtil.success("审核已提交") : ResultUtil.fail("提交失败");
    }

    @Override
    public Result deleteStockIn(Integer id) {
        int rows = stockInDetailDao.deleteStockInDetail(id);
        int rows1 = stockInDao.deleteStockIn(id);
        int a=rows+rows1;
        return a > 0 ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @Override
    public Result batchStockIn(List<StockInDetail> stockInDetails) {
        int i = 0;
        for (StockInDetail stockInDetail : stockInDetails) {
            StockInApproval stockInApproval = new StockInApproval();
            stockInApproval.setProductId(stockInDetail.getProductId());
            stockInApproval.setQuantity(stockInDetail.getQuantity());
            stockInApproval.setPrice(stockInDetail.getPrice());
            stockInApproval.setAmount(stockInDetail.getAmount());
            stockInApproval.setSupplier(stockInDetail.getSupplier());
            stockInApproval.setStatus(0);
            stockInApproval.setCreateTime(new Date());
            stockInApproval.setRemark(stockInDetail.getRemark());
            int rows = stockInApprovalDao.insertStockInApproval(stockInApproval);
            i += rows;
        }
        return i > 0 ? ResultUtil.success("批量提交成功") : ResultUtil.fail("提交失败");
    }

}
